<?php
/**
 * 路由器
 * 
 * 控制程序访问文件地址，集中管理URL请求类,简单功能.完善中.. ...
 * 
 * @category   Framework
 * @package    Controller
 * @author     Xinze <xinze@live.cn>
 * @copyright  Copyright (c) 2010, 黄新泽
 * @version    1.0
 * @todo       
 */
class Zero_Router extends Zero_Controller
{

    /**
     * App控制器
     * 
     * @access public
     * @var string|null
     */
    public $controller = null;

    /**
     * Constructor
     *
     * @param  string $ctl 控制器目录
     * @param  string $met 控制器方法
     * @param  string $typ 返回数据类型
     * @access public
     */
    public function __construct(&$ctl='Main_Index', $met='index', $typ='e')
    {
        parent::__construct($ctl, $met, $typ);
    }

    /**
     * 路由启动，返回控制器结果
     *
     * @var array  Controllers::dataRows  返回Ajax数据格式
     * @return array $rs
     * @access public
     */
    public function service()
    {
        $rs = $this->getData();

        return $rs;
    }

    /**
     * 设置控制器路径
     * 
     * @param string $ctl 控制器目录
     * @param string $index 运行文件
     * @return string path
     * @access    public
     */
    public function setPath(&$ctl='main', &$act='index')
    {
        $this->path = CTL_PATH . '/' . implode('/', explode('_', $ctl)) . '.php';
        $this->ctl = &$ctl;

        return $this->path;
    }

    /**
     * 取得控制器路径
     * 
     * @return string path
     * @access    public
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * 取得控制器类名称
     * 
     * @return string $this->className
     * @access    public
     */
    public function getClassName()
    {
        return $this->className;
    }

    /**
     * 得到Ajax数据格式
     *
     * @return    boolarray $data_rows  Ajax格式数据
     * @access    public
     * @see       Zero_Data
     */
    public function getData()
    {
        if (file_exists($this->path))
        {
            try
            {
                $this->controller = new $this->className($this->ctl, $this->met, $this->typ);
                $this->controller->run();

                if ('o' == $this->typ)
                {
                    $d = $this->controller->getDataRows();
                }
                else
                {
                    $d = trim(ob_get_contents());

                    ob_end_clean();
                    ob_start();
                }

                if (array_key_exists('data_type', $_REQUEST))
                {
                    if ('pack' == $_REQUEST['data_type'])
                    {
                        if (function_exists('msgpack_pack'))
                        {
                            return msgpack_pack($d);
                        }
                        else
                        {
                            return MsgPack_Coder::encode($d);
                        }
                    }
                    elseif ('amf' == $_REQUEST['data_type'])
                    {
                        return amf_encode(array(
                        'serviceName',
                        'methodName',
                        'message' => 'Hello, world!'
                        ));


                        /*
                        // Create a simple data structure
                        $data = array(
                        'serviceName',
                        'methodName',
                        'message' => 'Hello, world!');
                        // Create an instance of an AMF Output Stream
                        $out = new Zend_Amf_Parse_OutputStream();
                        // We will serialize our content into AMF3 for this example
                        // You could alternatively serialize it as AMF0 for legacy
                        // Flash applications.
                        $s = new Zend_Amf_Parse_Amf3_Serializer($out);
                        $s->writeObject($data);

                        // Return the content (we have found the newline is needed
                        // in order to process the data correctly on the client side)
                        return  "\n" . $out->getStream();
                        */
                    }
                    else
                    {
                        if ('o' == $this->typ)
                        {
                            return json_encode($d);
                        }
                        else
                        {
                            return $d;
                        }
                    }
                }
                else
                {
                    return $d;
                }
            }
            catch(Exception $e)
            {
                if ('cli' != SAPI)
                {
                    echo '<pre>';
                }

                print_r($e);
            }
        }
        else
        {
            //$data_rows = array('body'=>'<div class="tips" style="font-weight:bold; margin: 3px 0 3px 0;">功能开发中.....</div> <div class="tips">file does not exists:' . $this->path . '</div>', 'head'=>array('error'=>true, 'msg'=>'请求发生错误', 'num'=>0));
            error_header(404, 'Page Not Found');
            die('文件 ' . $this->path . ' does not exists');
        }
    }
}
?>